package com.wanlian.router.ui.main;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.smart.android.image.ImageLoader;
import com.smart.android.ui.web.WebActivity;
import com.wanlian.router.R;
import com.wanlian.router.bean.Banner;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Hyu on 2018/9/3.
 * Email: fvaryu@qq.com
 */
public class BannerAdapter extends RecyclerView.Adapter<BannerAdapter.BannerHolder> {
    private List<Banner> mBanners = new ArrayList<>(5);


    public void add(List<Banner> banners) {
        mBanners.clear();
        mBanners.addAll(banners);
        notifyDataSetChanged();
    }

    public List<Banner> getBanners() {
        return mBanners;
    }

    @NonNull
    @Override
    public BannerHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        return new BannerHolder(LayoutInflater.from(viewGroup.getContext())
                .inflate(R.layout.layout_item_banner, viewGroup, false)
        );
    }

    @Override
    public void onBindViewHolder(@NonNull BannerHolder bannerHolder, int i) {
        ImageLoader.load(bannerHolder.mImageView.getContext(),mBanners.get(i).getImgPath(), bannerHolder.mImageView);

        bannerHolder.itemView.setTag(mBanners.get(i));
        bannerHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Banner banner = (Banner) v.getTag();
                if (banner != null && !TextUtils.isEmpty(banner.getAttribute1())) {
                    WebActivity.fire(v.getContext(), banner.getAttribute1());
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return mBanners.size();
    }

    class BannerHolder extends RecyclerView.ViewHolder {

        ImageView mImageView;
        public BannerHolder(@NonNull View itemView) {
            super(itemView);
            mImageView =  itemView.findViewById(R.id.banner);
        }
    }
}
